package cn.electronic.sale.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Random;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.omg.CORBA.PRIVATE_MEMBER;
import org.omg.CORBA.PUBLIC_MEMBER;
import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import cn.electronic.sale.pojo.Customer;
import cn.electronic.sale.pojo.GoodsCustom;
import cn.electronic.sale.pojo.Orders;
import cn.electronic.sale.pojo.Ordersdetail;

/**
 * 类描述：订单控制类
 * 
 * @author: 陈明俊
 * @date： 日期：2017-1-18 时间：上午11:27:49
 * @version 1.0
 */
@Controller
@RequestMapping("/orders")
public class OrdersController extends BasicController {
	

	/**
	 * 传入订单
	 * 
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("ordersdetail")
	public String insertOrdersDetail(Ordersdetail ordersdetail,
			HttpSession session) throws Exception {
		System.out.println(ordersdetail);
		ordersdetail.setOrderDate(new Date());
		ordersdetail.setOrderStates("未付款");
		ordersDetailService.insertOrdersDetail(ordersdetail);
		return "orders/pay";
	}

	/**
	 * 查看购物车页面
	 * 
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/shoppingcarshow")
	public String ordersCarShow(HttpSession session, HttpServletRequest request)
			throws Exception {
		Customer customer = (Customer) session.getAttribute("customer");

		List<GoodsCustom> goodsCustoms = goodsService.selectAllGoodsInfo();
		ArrayList<GoodsCustom> goodsCustoms4 = new ArrayList<GoodsCustom>();
		int i = 0;
		for (GoodsCustom goodsCustom2 : goodsCustoms) {
			if (i < 4) {
				goodsCustoms4.add(goodsCustom2);
			}
			if (i == 4) {
				break;
			}
			i++;
		}
		session.setAttribute("goodsCustoms4", goodsCustoms4);

		// 查看该用户内购物车所有商品
		// 该购物车内的所有商品
		List<GoodsCustom> goodsCustoms2 = ordersService
				.selectAllGoodsInfoByCustomerId(customer.getCustomerId());
		int sum = 0;
		for (GoodsCustom goodsCustom2 : goodsCustoms2) {
			sum += goodsCustom2.getGoodsPrice();
		}
		session.setAttribute("goodsCustoms2", goodsCustoms2);
		session.setAttribute("sum", sum);
		System.out.println(goodsCustoms2);

		return "orders/order";

	}

	/**
	 * 购物车页面点击删除 删除订单商品 返回购物车
	 * 
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/shoppingcardel")
	public String ordersCarDelete(
			@RequestParam(value = "id", required = true) Integer goodsId,
			HttpSession session, HttpServletRequest request) throws Exception {
		Customer customer = (Customer) session.getAttribute("customer");

		System.out.println("test");
		ordersService.deleteByGoodsAndCustomerId(goodsId,
				customer.getCustomerId());
		System.out.println("成功");
		return "redirect:/orders/shoppingcarshow.action";

	}

	/**
	 * 加入购物车并跳转的购物车页面
	 * 
	 * 
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/shoppingcar")
	public String ordersCar(
			@RequestParam(value = "id", required = true) Integer goodsId,
			HttpSession session, HttpServletRequest request) throws Exception {

		// 插入要加入购物车的商品信息
		GoodsCustom goodsCustom = goodsService
				.selectAllGoodsInfoByPrimaryKey(goodsId);
		// session.setAttribute("goodsCustom",goodsCustom);
		// request.setAttribute("goodsCustom",goodsCustom);
		Customer customer = (Customer) session.getAttribute("customer");
		Orders order = new Orders();
		order.setCustomerId(customer.getCustomerId());
		order.setGoodsId(goodsCustom.getGoodsId());
		order.setOrderPrice(goodsCustom.getGoodsPrice());
		order.setOrderState("未付款");
		order.setOrderTime(new Date());
		ordersService.insertOrders(order);

		return "redirect:/orders/shoppingcarshow.action";
	}

	/**
	 * 单个购买跳转到订单页面
	 * 
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/buy")
	public String ordersUI(
			@RequestParam(value = "id", required = false) Integer goodsId,
			HttpSession session, HttpServletRequest request) throws Exception {
		ArrayList<GoodsCustom> goodsCustoms = new ArrayList<GoodsCustom>();
		if (goodsId != null) {
			GoodsCustom goodsCustom = goodsService
					.selectAllGoodsInfoByPrimaryKey(goodsId);
			goodsCustoms.add(goodsCustom);
			Integer sum = (Integer) session.getAttribute("sum");
			sum = goodsCustom.getGoodsPrice();
			session.setAttribute("sum", sum);
		} else {
			@SuppressWarnings("unchecked")
			List<GoodsCustom> goodsCustom = (List<GoodsCustom>) session
					.getAttribute("goodsCustoms2");
			for (GoodsCustom goodsCustom2 : goodsCustom) {
				goodsCustoms.add(goodsCustom2);
			}

		}
		System.out.println(goodsCustoms);
		session.setAttribute("goodsCustoms", goodsCustoms);
		request.setAttribute("goodsCustoms", goodsCustoms);

		return "orders/buy";
	}

}
